<?php
/**
 * 接口文档入口
 * 
 * @title 接口文档入口
 * @command pass
 * 
 * @desc	API辅助控制器
 * @version 1.0.0 
 * @author  Jason
 * @date	2017年4月26日 下午5:01:34
 * 
 * @author Jason
 * @version 1.0
 * @date	2017年6月26日 上午10:17:36
 */
class Comment extends MY_Manager_Controller
{
	/** @var Lib_docomment */
	public $lib_docomment = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_docomment');
	}
	
	function index() {
		$this->actionIndex();
	}
	
	// 处理接收 controllers 根目录参数
	// # ($directory = 'manage') 如果这样不太好处理根目录的情况，所以把默认值定为根目录比较合适
	public function actionIndex($directory = '.')
	{
		// MARK 先根据目录注册自动加载函数
		Lib_docomment::spl_autoload_register_controller($directory);
		
		# $directory = $directory ? : basename(dirname(__FILE__));这句没用了
		$dirName = dirname(dirname(__FILE__)) . "/" . $directory;
		
		$classNames = Lib_docomment::getClassNames($dirName);
		//print_r($classNames);
		
		echo "<pre>";
		foreach ($classNames as $className) {
			$classObject = Lib_docomment::getClassObject($className);
			$docComment = Lib_docomment::getClassComment($classObject);
			$commands = explode(" ", Lib_docomment::getParam($docComment, 'command'));
			if(in_array('pass', $commands)){
				continue;
			}
			$showDoc = Lib_docomment::filterParams($docComment, Lib_docomment::$innerItems);//array('command', 'title', 'author')
			// $url = $this->url("doc.fileDocument", array("className"=>$className));
			$url = site_url("document/comment/actionFileDocument/$className/$directory");
			$url = rtrim($url, '/.');
			$title = Lib_docomment::getParam($docComment, 'title');
			echo "{$showDoc}<a href='{$url}' target='_blank'>{$title}</a><br><hr>";
		}
		
		
	}
	
	// version 1.1.0 ($directory, $className) 由于 $directory 可以默认值，所以就应该放后面做为最后一个参数
	public function actionFileDocument($className, $directory = '.')
	{
		// MARK 先根据目录注册自动加载函数
		Lib_docomment::spl_autoload_register_controller($directory);
		
		$classObject = Lib_docomment::getClassObject($className);
		$methodObjects = Lib_docomment::getMethodObjects($classObject);
		
		echo "<pre>";
		foreach ($methodObjects as $methodObject) {
			$docComment = Lib_docomment::getMethodComment($methodObject);
			$structUrl = Lib_docomment::structUrl($classObject, $methodObject);
			$showDoc = Lib_docomment::filterParams($docComment, Lib_docomment::$innerItems);
			$commands = explode(" ", Lib_docomment::getParam($docComment, 'command'));
			if(in_array('pass', $commands)){
				continue;
			}
			
			echo "\n\n    {$showDoc}";
			if(in_array('noneurl', $commands)){
				echo "<br><hr>";
				continue;
			}
			echo "\n    数据结构: <a href='{$structUrl}' target='_blank'>{$structUrl}</a><br><hr>";
		}
	}
	
	
}